const express = require('express');
const Comment = require('../model/Comment')
const router = express.Router()
router.post('/comment', async (req, res) => {
    const newUser = await new Comment(req.body).save()
    console.log(newUser);
    res.send({
        status: 200,
        msg: '评论成功！'
    })
})
router.post('/commentChildren', async (req, res) => {
    let fatherCommentId = await Comment.findOne({_id: req.body._id})
    if (!fatherCommentId) {
        res.send({
            status: 400,
            msg: '没有对应的父级评论'
        })
    } else {
        fatherCommentId.children.push(req.body.children)
        let b = await Comment.update({_id: fatherCommentId._id},{$set:{children: fatherCommentId.children}})
        if (!b){
            res.send({
                status: 400,
                msg: '评论失败'
            })
        }
        else {
            res.send({
                status: 200,
                msg: '评论成功！'
            })
        }
    }
})
router.get('/getcomment', async(req, res) => {
    var b = await req.param("pageNumber")
    console.log(b);
    var commentList = []
    var comment = await Comment.find()
    var tishi = '加载更多'
    if (parseInt(b) >= parseInt(comment.length)){
        b = parseInt(comment.length)
        tishi = '没有更多了哦'
    }else {
        tishi = '加载更多'
    }
    console.log(comment.length);
    for ( var a = comment.length - 1 ; a > comment.length - 1 - b; a-- ){
        commentList.push(comment[a])
    }
    res.send(
        {
            commentList,
            status: 200,
            tishi
        }
    )
})
router.get('/commentNumber', async(req, res) => {
    const commentCount = await Comment.find()
    console.log(commentCount.length);
    res.send({
        commentNumber: commentCount.length
    })
})
module.exports = router
